import os

import paddleocr as ocr
from paddleocr import PaddleOCR

from base.ocr_base_tool import formating_recognized_texts, extract_info, crop_and_save_image


class Cover:
    def __init__(self, address=None, registration_number=None):
        self._address = address
        self._registration_number = registration_number
        self._paddle_ocr = PaddleOCR(use_angle_cls=True, lang="ch", max_text_length=64)

    @property
    def address(self):
        return self._address

    @address.setter
    def address(self, value):
        self._address = value

    @property
    def registration_number(self):
        return self._registration_number

    @registration_number.setter
    def registration_number(self, value):
        self._registration_number = value

    def recognize_cover(self, image_path):
        # 使用paddleocr识别图像中的文本
        result = self._paddle_ocr.ocr(image_path, det=True, rec=True)

        #  ---   打印结果
        for idx in range(len(result)):
            res = result[idx]
            if res is None:
                continue
        ocr_result_pages_unit = result
        recognized_line_list_in_one_page = ocr_result_pages_unit[0]

        # 规范化解析文字
        (whole_strings_in_one_page, info_list_in_one_page) = formating_recognized_texts(
            recognized_line_list_in_one_page)

        print(whole_strings_in_one_page)

        self._registration_number = extract_info(info_list_in_one_page, '登记字号：')
        self._address = extract_info(info_list_in_one_page, '坐落：')
        print(f'登记字号：{self._registration_number}')
        print(f'坐落：{self._address}')

        # self.address = result[0][1] if result else None
        # self.registration_number = result[1][1] if len(result) > 1 else None

    def recognize_registration_number(self, image_path):
        # 先截取图片再识别
        # 一定要有一定的扩展，否则会识别不出来的
        coordinate_on_image = [975, 147, 1465, 188]

        cropped_image_path = os.path.join(self.target_dir,
                                          f'{os.path.splitext(image_path)[0]}_cropped' + os.path.splitext(image_path)[
                                              1])

        # 截取指定位置
        crop_and_save_image(image_path, coordinate_on_image, cropped_image_path)

        # 使用PaddleOCR识别图片中的文字
        result = self._paddle_ocr.ocr(cropped_image_path, cls=True)
        # 假设水印位于左上角，取识别结果的第一个元素
        if result:
            # print(result[0])
            # print(result[0][0])
            # print(result[0][0][1])
            self._registration_number = result[0][0][1][0]

            ##################################################################################
            # 删除裁剪后的图片
            os.remove(cropped_image_path)

    def recognize_address(self, image_path):
        # 先截取图片再识别
        # 一定要有一定的扩展，否则会识别不出来的
        coordinate_on_image = [475, 803, 1436, 863]

        cropped_image_path = os.path.join(self.target_dir,
                                          f'{os.path.splitext(image_path)[0]}_cropped' + os.path.splitext(image_path)[
                                              1])

        # 截取指定位置
        crop_and_save_image(image_path, coordinate_on_image, cropped_image_path)

        # 使用PaddleOCR识别图片中的文字
        result = self._paddle_ocr.ocr(cropped_image_path, cls=True)
        # 假设水印位于左上角，取识别结果的第一个元素
        if result:
            # print(result[0])
            # print(result[0][0])
            # print(result[0][0][1])
            self._address = result[0][0][1][0]

            ##################################################################################
            # 删除裁剪后的图片
            os.remove(cropped_image_path)

